<template>
  <!-- 新增部门的弹层 -->
  <el-dialog
    :title="formData.id ? '编辑' : '新增'"
    :visible="showDialog"
    @close="isClose"
  >
    <el-form ref="form" label-width="120px" :model="formData" :rules="rules">
      <el-form-item label="部门名称" prop="name">
        <el-input v-model="formData.name" style="width:80%" placeholder="2-50个字符" />
      </el-form-item>
      <el-form-item label="部门编码" prop="code">
        <el-input v-model="formData.code" style="width:80%" placeholder="2-50个字符" />
      </el-form-item>
      <el-form-item label="部门负责人" prop="manager">
        <el-select v-model="formData.manager" style="width:80%" placeholder="请选择" @focus="getEmployeeSimple">
          <el-option v-for="item in peoples " :key="item.id" :label="item.username" :value="item.username" />
        </el-select>
      </el-form-item>
      <el-form-item label="部门介绍" prop="introduce">
        <el-input v-model="formData.introduce" style="width:80%" placeholder="2-300个字符" type="textarea" :rows="3" />
      </el-form-item>
    </el-form>
    <!-- el-dialog有专门放置底部操作栏的 插槽  具名插槽 -->
    <el-row slot="footer" type="flex" justify="center">
      <!-- 列被分为24 -->
      <el-col :span="8">
        <el-button type="primary" size="small" @click="isOK">确定</el-button>
        <el-button size="small" @click="isClose">取消</el-button>
      </el-col>
    </el-row>
  </el-dialog>
</template>

<script>
import { getDepartments, addDepartments, getDepartDetail, updateDepartments } from '@/api/departments'
import { getEmployeeSimple } from '@/api/employees'
export default {
  props: {
    showDialog: {
      type: Boolean,
      default: false
    },
    treeNode: {
      type: Object,
      default: null
    }
  },
  data() {
    const sameName = async(rule, value, callback) => {
      // 1.获取组织部门
      // 2.寻找同层部门
      // 3.同层部门名称相同的筛选出来
      const { depts } = await getDepartments()
      let res
      if (this.formData.id) { // 编辑
        res = depts.some(item => item.id !== this.formData.id && item.pid === this.treeNode.pid && item.name === value)
      } else { // 新增
        //   const res = depts.filter(item => item.pid === this.treeNode.id).some(item => item.name === value)
        res = depts.some(item => item.pid === this.treeNode.id && item.name === value)
      }
      res ? callback(new Error(`同级部门下已经有${value}的部门了`)) : callback()
    }
    const sameCode = async(rule, value, callback) => {
      const { depts } = await getDepartments()
      let res
      if (this.formData.id) {
        res = depts.some(item => item.id !== this.formData.id && item.code === value)
      } else {
        res = depts.some(item => item.code === value)
      }
      res ? callback(new Error(`同级部门下已经有${value}的编码了`)) : callback()
    }
    return {
      formData: {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      },
      rules: {
        name: [{ required: true, message: '部门名称不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '部门名称要求2-50个字符', trigger: 'blur' },
          { validator: sameName, trigger: 'blur' }],
        code: [{ required: true, message: '部门编码不能为空', trigger: 'blur' },
          { min: 2, max: 50, message: '部门编码要求2-50个字符', trigger: 'blur' },
          { validator: sameCode, trigger: 'blur' }],
        manager: [{ required: true, message: '部门负责人不能为空', trigger: 'blur' }],
        introduce: [{ required: true, message: '部门介绍不能为空', trigger: 'blur' },
          { trigger: 'blur', min: 2, max: 300, message: '部门介绍要求2-300个字符' }]
      },
      peoples: []
    }
  },
  methods: {
    async getEmployeeSimple() {
      this.peoples = await getEmployeeSimple()
    //   console.log(this.peoples)
    },
    async  isOK() {
      await this.$refs.form.validate() // 判断是否校验成功
      if (this.formData.id) {
        await updateDepartments({ ...this.formData })
      } else {
        await addDepartments({ ...this.formData, pid: this.treeNode.id })
      }
      this.$emit('getDepts')
      this.$message.success(this.treeNode.id ? '编辑成功' : '新增成功')
      this.$emit('update:show-dialog', false)
    },
    isClose() { // 关闭弹窗 就会触发  点击按钮或者x
      // 1.关闭弹窗
      this.$emit('update:show-dialog', false)
      // 2. 清空校验
      this.$refs.form.clearValidate()
      // 3. 重置表单数据
      this.formData = {
        name: '', // 部门名称
        code: '', // 部门编码
        manager: '', // 部门管理者
        introduce: '' // 部门介绍
      }
    },
    async  getDepartDetail(id) {
      this.formData = await getDepartDetail(id)
      console.log(this.formData)
      console.log(this.treeNode)
    //   console.log(this.formData)
    }
    // async  getDepartDetail() {
    //   this.formData = await getDepartDetail(this.treeNode.id)
    // }

  }
}
</script>

<style>

</style>
